/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. Camunda licenses this file to you under the Apache License,
 * Version 2.0; you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.data.modules.bpm.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.data.common.util.RedisUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.camunda.commons.utils.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * A thread-safe LRU {@link Cache} with a fixed capacity. If the cache reaches
 * the capacity, it discards the least recently used entry first.
 * <p>
 * *Note*: The consistency of the keys queue with the keys in the cache is not ensured! This means, the keys queue
 * can contain duplicates of the same key and not all the keys of the queue are necessarily in the cache.
 * However, all the keys of the cache are at least once contained in the keys queue.
 *
 * @param <K> the type of keys
 * @param <V> the type of mapped values
 */
@Slf4j
@Component("com.data.modules.bpm.redis.ConcurrentLruRedis")
public class ConcurrentLruRedis<K, V> implements Cache<K, V> {
  private static final String BPM_REDIS = "BPM:CACHE:";
  
  @Autowired
  @Lazy
  private RedisUtil redisUtil;

  public ConcurrentLruRedis(){
    log.info("init--ConcurrentLruRedis");
  }
  
  @SneakyThrows
  @Override
  public V get(K key) {
    //TODO redis
    Object obj = redisUtil.get(BPM_REDIS + key);
    if(obj == null) return null;
    Object tmp = null;
    try{
      String pde = String.valueOf(obj);
      String[] strs = pde.split("繁体字DTO");
      String dto = strs[0];
      String dtoMsg = strs[1];
      Class<?> clz = Class.forName(dto);
      tmp = JSONObject.toJavaObject(JSON.parseObject(dtoMsg), clz);
    }catch (Exception e){
      log.error(e.getMessage(),e);
    }
    //V value = JSON.toJavaObject(dtoMsg,clz.getClass());
    return (V) tmp;
  }

  @Override
  public void put(K key, V value) {
    if (key == null || value == null) {
      throw new NullPointerException();
    }
    Class<?> clz = value.getClass();
    String name = clz.getName();
    
    //TODO redis
    try{
      String msg = JSON.toJSONString(value);
      StringBuffer sb = new StringBuffer(name);
      sb.append("繁体字DTO").append(msg);
      log.info("put msg:{}",sb.toString());
      redisUtil.set(BPM_REDIS + key,sb.toString());
      
    }catch (Exception e){
      log.error("dto:{}",name);
      log.error(e.getMessage(),e);
    }
  }

  @Override
  public void remove(K key) {
    //TODO redis
    redisUtil.del(BPM_REDIS + key);
  }

  @Override
  public void clear() {
    //TODO redis
    redisUtil.removeAll(BPM_REDIS);
  }

  @Override
  public boolean isEmpty() {
    Set<K> tmp = keySet();
    if(tmp == null){
      return true;
    }
    return false;
  }

  @Override
  public Set<K> keySet() {
    return (Set<K>) redisUtil.keys(BPM_REDIS);
  }

  @Override
  public int size() {
    Set<K> tmp = keySet();
    if(tmp == null){
      return 0;
    }
    return tmp.size();
  }

}
